package com.iqiyi.pps.epg.core.service.auth;

import com.iqiyi.pps.epg.core.dao.auth.AuthorityDao;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.model.auth.Authority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 权限管理
 *
 */

@Service
@Transactional
public class AuthorityManager{
	@Autowired
	private AuthorityDao authorityDao;

    /**
     * 根据主键取Authority对象
     * @param id  主键ID
     * @return Authority
     */
	@Transactional(readOnly = true)
	public Authority getAuthority(Long id) {
		return authorityDao.get(id);
	}

    /**
     * 取得所有权限
     * @return List<Authority>
     *
     */
	public List<Authority> getAllAuthority() {
		List<Authority> authorityList = authorityDao.getAll();
		return authorityList;
	}

    /**
     * 保存authority入库
     * @param authority Authority对象
     */
	public void saveAuthority(Authority authority) {
		authorityDao.save(authority);
	}

    /**
     * 根据主键删除记录
     * @param id 主键ID
     */
	public void deleteAuthority(Long id) {
		authorityDao.delete(id);
	}

    /**
     * 查询权限
     * @param page Page<Authority>
     * @param filters 查询条件
     * @return Page<Authority>
     */
	 @Transactional(readOnly = true)
     public Page<Authority> searchAuthority(final Page<Authority> page, final List<PropertyFilter> filters){
          return authorityDao.findPage(page, filters);
     }

    /**
     * 根据权限name，获得Authority
     * @param name 权限name
     * @return Authority
     */
	public Authority getAuthorityByName(String name) {
		return authorityDao.getAuthorityByName(name);
	}

    /**
	 * 检查授权是否唯一.
	 * @param newAuthrityName AuthrityName
     * @param oldAuthrityName AuthrityName
	 * @return name在数据库中唯一或等于oldAuthrityName时返回true.
	 */
	@Transactional(readOnly = true)
	public boolean isAuthrityNameUnique(String newAuthrityName, String oldAuthrityName) {
		return authorityDao.isPropertyUnique("name", newAuthrityName, oldAuthrityName);
	}


}

